package com.yaoweigroup.community.ui.bm;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.overlay.DrivingRouteOverlay;
import com.amap.api.maps2d.overlay.WalkRouteOverlay;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.RouteSearch.DriveRouteQuery;
import com.amap.api.services.route.RouteSearch.OnRouteSearchListener;
import com.amap.api.services.route.RouteSearch.WalkRouteQuery;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;
import com.yaoweigroup.community.CApplication;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Common;
import com.yaoweigroup.community.util.DialogUtil;
import com.yaoweigroup.community.util.LogUtil;
import com.yaoweigroup.community.util.Utils;

public class RouteActivity extends Activity implements OnClickListener,
		OnRouteSearchListener {

	private TextView reg_tv;
	private ImageButton left_ib;
	private TextView center_tv;

	private String cityName;
	private RelativeLayout left_rl;

	private MapView mapView;
	private RouteSearch routeSearch;

	private AMap aMap;
	private TextView second_tv;
	private ProgressDialog mRouteCalculatorProgressDialog;// 路径规划过程显示状态

	private Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		Common.PROPORTION = width / 320f;
		Common.WIDTH = width;

		// 创建Demo视图
		setContentView(R.layout.activity_nav);
		mapView = (MapView) findViewById(R.id.map);
		mapView.onCreate(savedInstanceState);// 此方法必须重写
		initRes();

	}

	private String getSdcardDir() {
		if (Environment.getExternalStorageState().equalsIgnoreCase(
				Environment.MEDIA_MOUNTED)) {
			return Environment.getExternalStorageDirectory().toString();
		}
		return null;
	}

	private LatLonPoint startPoing;
	private LatLonPoint endPoint;

	private void initRes() {
		if (aMap == null) {
			aMap = mapView.getMap();
			// registerListener();
		}
		dialog = DialogUtil.createWaitDialog(this, "线路规划中");
		reg_tv = (TextView) findViewById(R.id.reg_tv);
		reg_tv.setOnClickListener(this);
		reg_tv.setText("驾车");
		center_tv = (TextView) findViewById(R.id.center_tv);
		center_tv.setText("线路规划");
		second_tv = (TextView) findViewById(R.id.second_tv);
		second_tv.setVisibility(View.VISIBLE);
		second_tv.setText("导  航");
		second_tv.setOnClickListener(this);
		second_tv.setVisibility(View.GONE);

		left_rl = (RelativeLayout) findViewById(R.id.left_rl);
		left_rl.setOnClickListener(this);
		routeSearch = new RouteSearch(this);
		routeSearch.setRouteSearchListener(this);

		double starLat = 31.347847501580;
		double starLng = 121.582241957700;
		if (CApplication.latLng != null) {
			startPoing = new LatLonPoint(CApplication.latLng.latitude,
					CApplication.latLng.longitude);
		}

		String lat = getIntent().getStringExtra("lat");
		String lng = getIntent().getStringExtra("lng");
		if (lat != null) {
			endPoint = new LatLonPoint(Double.parseDouble(lat),
					Double.parseDouble(lng));
			if (dialog != null)
				dialog.show();
			searchRouteResult(startPoing, endPoint, 2);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		mapView.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mapView.onResume();

	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}

	/**
	 * 方法必须重写
	 */
	public void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();

	}

	private int type = 2;

	private void searchRouteResult(LatLonPoint startPoint,
			LatLonPoint endPoint, int type) {
		final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
				startPoint, endPoint);
		if (type == 2) {// 乘车
			DriveRouteQuery query = new DriveRouteQuery(fromAndTo,
					RouteSearch.DrivingDefault, null, null, "");// 第一个参数表示路径规划的起点和终点，第二个参数表示驾车模式，第三个参数表示途经点，第四个参数表示避让区域，第五个参数表示避让道路
			routeSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询
		} else {
			WalkRouteQuery query = new WalkRouteQuery(fromAndTo,
					RouteSearch.WalkDefault);
			routeSearch.calculateWalkRouteAsyn(query);// 异步路径规划步行模式查询
		}

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_rl:
			finish();
			break;
		case R.id.reg_tv:
			if (type == 2) {
				type = 3;
				reg_tv.setText("步行");
			} else {
				type = 2;
				reg_tv.setText("驾车");
			}
			dialog.show();
			searchRouteResult(startPoing, endPoint, type);
			break;
		case R.id.second_tv:

			break;
		}

	}

	@Override
	public void onBusRouteSearched(BusRouteResult arg0, int arg1) {

	}

	private DriveRouteResult driveRouteResult;

	@Override
	public void onDriveRouteSearched(DriveRouteResult result, int rCode) {
		if (dialog != null)
			dialog.dismiss();
		if (rCode == 0) {
			if (result != null && result.getPaths() != null
					&& result.getPaths().size() > 0) {
				driveRouteResult = result;
				DrivePath drivePath = driveRouteResult.getPaths().get(0);
				aMap.clear();// 清理地图上的所有覆盖物
				DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
						this, aMap, drivePath, driveRouteResult.getStartPos(),
						driveRouteResult.getTargetPos());
				drivingRouteOverlay.removeFromMap();
				drivingRouteOverlay.addToMap();
				drivingRouteOverlay.zoomToSpan();
			} else {
				Toast.makeText(RouteActivity.this, "没有线路", 500).show();
			}
		} else if (rCode == 27) {
			Toast.makeText(RouteActivity.this, "网络错误", 500).show();
		} else {
			Toast.makeText(RouteActivity.this, "未知错误" + rCode, 500).show();
		}
	}

	private WalkRouteResult walkRouteResult;

	@Override
	public void onWalkRouteSearched(WalkRouteResult result, int rCode) {
		if (dialog != null)
			dialog.dismiss();
		if (rCode == 0) {
			if (result != null && result.getPaths() != null
					&& result.getPaths().size() > 0) {
				walkRouteResult = result;
				WalkPath walkPath = walkRouteResult.getPaths().get(0);
				aMap.clear();// 清理地图上的所有覆盖物
				WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(this,
						aMap, walkPath, walkRouteResult.getStartPos(),
						walkRouteResult.getTargetPos());
				walkRouteOverlay.removeFromMap();
				walkRouteOverlay.addToMap();
				walkRouteOverlay.zoomToSpan();
			} else {
				Toast.makeText(RouteActivity.this, "没有线路", 500).show();
			}
		} else if (rCode == 27) {
			Toast.makeText(RouteActivity.this, "网络错误", 500).show();
		} else {
			Toast.makeText(RouteActivity.this, "未知错误" + rCode, 500).show();
		}
	}

}
